package com.kzd.btgame.main.mine

import android.graphics.Rect
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.btgame.app.URL_PAY_MEMBER
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.custom.setTransitionStatus
import com.kzd.btgame.databinding.FragmentMemberCenterBinding
import com.kzd.btgame.jetpack.vm.MineViewModel
import com.kzd.btgame.main.mine.adapter.MemberCenterCardAdapter
import com.kzd.btgame.main.mine.adapter.MemberCenterContentAdapter
import com.kzd.btgame.main.popup.PaymentBottomPopup
import com.kzd.btgame.main.toMineRichTextActivityUrl
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class MemberCanterFragment : OwnerViewBindingFragment<FragmentMemberCenterBinding>() {

    companion object {
        fun newInstance(): MemberCanterFragment {
            return MemberCanterFragment()
        }
    }

    private val mMemberCenterCardAdapter = MemberCenterCardAdapter()

    private val mMemberCenterContentAdapter = MemberCenterContentAdapter()

    private val mPaymentBottomPopup: PaymentBottomPopup by lazy {
        PaymentBottomPopup(requireContext(), onCenterConfig = { _, type, centerConfig ->
            mViewModel.mineMemberCenterOrder(
                centerConfig.id, centerConfig.money, type.toString()
            )
        })
    }

    private val mViewModel:MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMemberCenterBinding
        get() = FragmentMemberCenterBinding::inflate

    override fun initView() {
        mViewBinding.rvMemberCenterCardBody.apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            if (itemDecorationCount > 0)
                return
            addItemDecoration(ItemDecoration())
        }.adapter = mMemberCenterCardAdapter.apply {
            setOnItemClickListener { _, _, position ->
                selectPosition = position
                val itemDate = getItem(position)
                mMemberCenterContentAdapter
                    .setNewInstance(itemDate.content)
                mViewBinding.tvMemberCenterPrice.text = StringBuilder().append("¥\t")
                    .append(itemDate.money)
            }
        }

        mViewBinding.rvMemberCenterContentBody.adapter = mMemberCenterContentAdapter

        mViewBinding.tvMemberCenterPayment.setOnClickAntiShakeListener {
            val centerConfig = mViewModel.memberCenterCardLiveData.value?.data?.data?.config
                ?: return@setOnClickAntiShakeListener
            if (centerConfig.isNullOrEmpty())
                return@setOnClickAntiShakeListener
            mPaymentBottomPopup.fillData(centerConfig[mMemberCenterCardAdapter.selectPosition])
                .showPopupWindow()
        }
    }

    override fun initData() {
        mViewModel.mineMemberCenterCard()
    }

    override fun initViewMode() {
        mViewModel.memberCenterCardLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data?.config
                ?: return@observe
            mMemberCenterCardAdapter.setNewInstance(data)

            if (data.size <= 0) {
                return@observe
            }
            mMemberCenterContentAdapter
                .setNewInstance(data[0].content)
            mViewBinding.tvMemberCenterPrice.text = StringBuilder().append("¥\t")
                .append(data[0].money)
        })
        mViewModel.centerOrderLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val url = StringBuilder(URL_PAY_MEMBER)
                .append("&id=").append(data).toString()
            toMineRichTextActivityUrl(
                title = "支付", url = url
            )
        })
    }


    inner class ItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
        ) {
            val position: Int = parent.getChildAdapterPosition(view)
            if (position == 0) {
                outRect.left = ConvertUtils.dp2px(15f)
            }
            if (position == state.itemCount - 1) {
                outRect.right = ConvertUtils.dp2px(15f)
            } else {
                outRect.right = ConvertUtils.dp2px(13f)
            }
        }
    }

}